home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / cmplib_s.lha / cmplib_src / $compile1.P < prev    next >
Text File  |  1990-04-12  |  9KB  |  277 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $compile1.P */
  25.  
  26. /* This Prolog program is a first attempt to take an input Prolog program in
  27. a simple syntax, relatively close to standard Prolog, and to output an
  28. annotated term, with pragmas attached, so that the pilgen translator can
  29. generate correct intermediate code. 
  30.  
  31. The input clauses will be read (using Prolog's read command) one at a time
  32. from standard input (for now), until end-of-file is reached.  
  33.  
  34. During the translation of a clause, a Symbol Table (St) is constructed.  It
  35. contains information on each occurrence of a variable in the clause, as
  36. follows.  It is a record st(Clist,Vlist); where Clist is the list of records
  37. c(Litno,Npars,Pragma) where Litno is the number of the literal on the rhs of
  38. the rule, Npars is the number of arguments to that literal, and Pragma is
  39. the pragma associated with the _call record for that literal in the clause;
  40. Vlist is a list of records, v(id,Occlist), one for each variable; where id
  41. the unique identifier for the variable, and Occlist is a list of occurrence
  42. records, o(Occno,Litno,Argno,Context,Pragma), one for each occurrence of the
  43. variable in the clause; where Occno uniquely determines the occurrence of
  44. this variable; Litno is the number of the literal (0=head, 1=first on
  45. rhs,...); Argno is the number of the argument in which it appears (starting
  46. with 1) Context is `s' if it is in a structure and `t' if it is at the top
  47. level; and Pragma is the list that is the Pragma for the occurrence of the
  48. variable in the code.  
  49. */ 
  50.  
  51. $compile_export([$compile/0,$compile/1,$compile/2,$compile/3,$compile/4]).
  52.  
  53. $compile_use($name,[$name/2,_]).
  54. $compile_use($blist,[$append/3,$member/2,$member1/2,$not_member1/2]).
  55. $compile_use($bio,[$writename/1,$writeqname/1,$put/1,$nl/0,$tab/1,$tell/1,
  56.     $telling/1,$told/0,$get/1,$get0/1,$see/1,$seeing/1,$seen/0]).
  57. $compile_use($bmeta,[$atom/1,$atomic/1,$integer/1,$number/1,$structure/1,
  58.     $functor0/2,$bldstr/3,$arg/3,$arity/2,$real/1,$floor/2]).
  59. $compile_use($osys,[$cputime/1,_,_,_]).
  60. $compile_use($compare,['$=='/2,_,_,_,_,_,_]).
  61. $compile_use($listutil1,[$reverse/2,$merge/3,$absmember/2,$absmerge/3,
  62.         $nthmember/3,$nthmember1/3,$member2/2,$closetail/1]).
  63. $compile_use($getclauses,[$getclauses/2,$getclauses/3,$attach/2,_]).
  64. $compile_use($mac,[$macexp/3]).
  65.  
  66. /* **********************************************************************
  67. $compile_use($aux1,[$roundtosq/2,$get_compiletime/2,$message1/1,
  68.             $output_msg/3,$umsg/1,$name_aslfile/2,_,_,_]).
  69. $compile_use($translate1,[$translateasm/3,$translate/3]).
  70. ********************************************************************** */
  71.  
  72.  
  73. $compile :- $writename('compile: Usage: compile(<inputfile> [,<outputfile>, [,<option>] ])'), $nl.
  74.  
  75. $compile(InFile) :-
  76.     $atomic(InFile) ->
  77.         ($name(InFile,Fname),
  78.          $append(Fname,".out",OFname),
  79.          $name(OutFile,OFname),
  80.          $compile_default_opts(Options),
  81.          $compile0(InFile,OutFile,Options,_)
  82.         ) ;
  83.         ($writename('*** Error: filenames must be atomic! ***'),
  84.          $nl,
  85.          abort
  86.         ).
  87.  
  88. $compile(InFile,Opt2) :-
  89.     $atomic(InFile) ->
  90.         ($atomic(Opt2), !, /* Opt2 is filename */
  91.          $compile_default_opts(Options),
  92.          $compile0(InFile,Opt2,Options,_)
  93.         ) ;
  94.         ($writename('*** Error: input filename must be atomic! ***'),
  95.          $nl,
  96.          abort
  97.         ).
  98. $compile(InFile,Opt2) :- /* Opt2 is Options */
  99.         $name(InFile,Fname),
  100.         $append(Fname,".out",OFname),
  101.         $name(OutFile,OFname),
  102.         $compile0(InFile,OutFile,Opt2,_).
  103.  
  104. $compile(InFile, Opt1, Opt2) :-
  105.     $atomic(InFile) ->
  106.         ($atomic(Opt1), !, /* Opt1 is filename */
  107.          $compile0(InFile,Opt1,Opt2,_)
  108.         ) ;
  109.         ($writename('*** Error: input filename must be atomic! ***'),
  110.          $nl,
  111.          abort
  112.                 ).
  113. $compile(InFile, Opt1, Opt2) :-
  114.     $atomic(Opt2) ->
  115.         ( /* Opt2 is filename */
  116.           $compile0(InFile,Opt2,Opt1,_)
  117.         ) ;
  118.         ( $writename('*** Error: output filename must be atomic! ***'),
  119.           $nl,
  120.           abort
  121.         ).
  122.  
  123. $compile(InFile, Opt1, Opt2,PredList) :-
  124.     $atomic(InFile) ->
  125.         ($atomic(Opt1), !, /* Opt1 is filename */
  126.           $compile0(InFile,Opt1,Opt2,PredList)
  127.         ) ;
  128.         ($writename('*** Error: input filename must be atomic! ***'),
  129.          $nl,
  130.          abort
  131.                 ).
  132. $compile(InFile, Opt1, Opt2,PredList) :-
  133.     $atomic(Opt2) ->
  134.         ( /* Opt2 is filename */
  135.           $compile0(InFile,Opt2,Opt1,PredList)
  136.         ) ;
  137.         ( $writename('*** Error: output filename must be atomic! ***'),
  138.           $nl,
  139.           abort
  140.         ).
  141.  
  142. /* default_opts([v]). */
  143.  
  144. $compile_default_opts([l]).
  145.  
  146. /* translate */
  147. $compile0(InFile, OutFile, Options, PredList) :- 
  148.     $nl, 
  149.         $writename( 'Compile : ' ), $writename( InFile ),  $writename(' => '),
  150.     $writename( OutFile ), $nl,
  151.     $exists(InFile) -> $compile1(InFile,OutFile,Options,PredList) ;
  152.                $umsg(['*** No such file:',InFile]).
  153.  
  154. $compile1(InFile, OutFile, Options, PredList) :- 
  155.     $init_indexinfo,
  156.     $cputime(X1),
  157.     ($member1(a,Options) -> /* create intermediate .asl file */
  158.           ($message1(Options),
  159.            $name_aslfile(InFile, OutFile1),
  160.            $compile_gen_PIL(InFile,OutFile1,Options,PredList),
  161.            $asm(OutFile1,OutFile,Options)
  162.          ) ;
  163.          $compile_gen_BC(InFile,OutFile,Options,PredList)
  164.     ),
  165.     ($member1(l, Options) ->  /* load byte code */
  166.         load(OutFile) ;
  167.         true
  168.     ),
  169.     $compile_cleanup,
  170.     $tell(user),
  171.     $cputime(X2),
  172.     CTime is X2 - X1, $get_compiletime(CTime,Time),
  173.     $writename('compilation complete -- '),
  174.     $writename(Time),$writename(' secs'),$nl.
  175.  
  176. $compile_gen_PIL(InFile, OutFile1, Options,PredList) :-
  177.     $getclauses(InFile,IProg,PredList),
  178.     $macexp(IProg,Options,Prog),
  179.     $compile_indexinfo(PredList,IndexInfo),
  180.     $compile_getmodes(Prog,Options,IndexInfo,PredList,Modes),
  181.     $tell(OutFile1),
  182.     !,
  183.     $compile_transl_preds(Prog,Options,IndexInfo,Modes,PredList),
  184.     $told.
  185.  
  186. $compile_transl_preds(ClauseList,Options,Prag,Modes,PredList) :-
  187.     $member(PredDef, ClauseList),
  188.     PredDef = pred(P,N,_,_,_),
  189.     $output_msg(P,N,Options),
  190.     ($member2([P,N,Mode],Modes) -> true ; $zerolist(N,Mode)),
  191.     $translate(PredDef,Options,Prag,Mode,PredList),
  192.     fail.
  193. $compile_transl_preds(_,_,_,_,_).
  194.  
  195. $compile_gen_BC(InFile, OutFile, Options,PredList) :-
  196.     $getclauses(InFile,IProg,PredList),
  197.     $macexp(IProg,Options,Prog),
  198.     $compile_indexinfo(PredList,IndexInfo),
  199.     $compile_getmodes(Prog,Options,IndexInfo,PredList,Modes),
  200.     $tell(OutFile),
  201.     $compile_translasm_preds(Prog,Options,IndexInfo,Modes,PredList),
  202.     $told.
  203.  
  204. $compile_translasm_preds(ClauseList,Options,Prag,Modes,PredList) :-
  205.     $member(PredDef, ClauseList),
  206.     PredDef = pred(P,N,_,_,_),
  207.     $output_msg(P,N,Options),
  208.     ($member2([P,N,Mode],Modes) -> true ; $zerolist(N,Mode)),
  209.     $translateasm(PredDef,Options,Prag,Mode,PredList),
  210.     fail.
  211. $compile_translasm_preds(_,_,_,_,_).
  212.  
  213. $compile_getmodes(Prog,Options,Prag,PredList,Modes) :-
  214.      ((symtype('_$mode'(_,_,_),MDef), MDef > 0) ->
  215.          $bagof([P,N,M],'_$mode'(P,N,M),Modes1) ;
  216.          Modes1 = []
  217.     ),
  218.     ($member1(m,Options) ->
  219.         ($mode_inf(Prog,Options,Prag,PredList,Modes0),
  220.          $rplc_mode(Modes1,Modes0,Modes)
  221.         ) ;
  222.         Modes = Modes1
  223.     ).
  224.  
  225. $rplc_mode([],M,M).
  226. $rplc_mode([M|MRest0],Modes1,Modes) :-
  227.     $rplc_mode1(Modes1,M,Modes2),
  228.     $rplc_mode(MRest0,Modes2,Modes).
  229.  
  230. $rplc_mode1([],M,[M]).
  231. $rplc_mode1([[P,N,Mode1]|Rest],[P,N,Mode0],[[P,N,Mode0]|Rest]) :- !.
  232. $rplc_mode1([M1|MRest],M0,[M1|NRest]) :- $rplc_mode1(MRest,M0,NRest).
  233.  
  234. $zerolist(0,[]).
  235. $zerolist(N,[0|L]) :- N > 0, N1 is N-1, $zerolist(N1,L).
  236.  
  237. $compile_indexinfo(PredList,Index) :-
  238.      $check_legit_index(PredList),
  239.      $compile_indexinfo1(PredList,Index).
  240.  
  241. $check_legit_index(Preds) :-
  242.      $bagof((P/N), K^'_$index'(P,N,K), IndexList),
  243.      $telling(OFile), $tell(user),
  244.      $check_legit1(IndexList,Preds),
  245.      $told, $tell(OFile).
  246.  
  247. $check_legit1([],_).
  248. $check_legit1([X|L],Preds) :-
  249.      ($member1(X,Preds) ->
  250.           true ;
  251.       (X = '/'(P,N),
  252.        $write('*** Warning: index set on undefined predicate '),
  253.        $write(P), $write('/'), $write(N), $nl
  254.       )
  255.      ),
  256.      $check_legit1(L,Preds).      
  257.  
  258. $compile_indexinfo1([],[]).
  259. $compile_indexinfo1([(P/N)|Preds],[prag(P,N,[index(K)])|IRest]) :-
  260.      ('_$index'(P,N,K) ->
  261.           true ;
  262.       K = 1
  263.      ),
  264.      $compile_indexinfo1(Preds,IRest).          
  265.  
  266. $init_indexinfo :-
  267.      $symtype('_$index'(_,_,_), L),
  268.      (L > 0 -> true ; $assert(('_$index'(_,_,_) :- fail))).
  269.  
  270. $compile_cleanup :-
  271.     $abolish('_$mode'(_,_,_)),
  272.     $abolish('_$emode'(_,_,_)),
  273.     $abolish('_$index'(_,_,_)).
  274.  
  275. /* ---------------------------- $compile1.P ---------------------------- */
  276.  
  277.